package com.samsung.common.util;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class EscapeTag extends SimpleTagSupport {
	private String value; // display  
	private String input; // text or textarea   input = 'true'
	private boolean nbspEscape = true;
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public String getInput() {
		return input;
	}
	public void setInput(String input) {
		this.input = input;
	}
	public void setNbspEscape(boolean nbspEscape) {
		this.nbspEscape = nbspEscape;
	}
 @Override
  public void doTag() throws IOException, JspException {

		String outputText = null;
		if ("true".equals(input)) {
			outputText = this.safeHtmlEscInput(value);
		} else if ("hidden".equals(input)) {
			outputText = this.safeHtmlEscHidden(value);
		} else {
			outputText = this.safeHtmlEscDisplay(value);
		}
		if (!nbspEscape) {
			outputText = outputText.replace("&nbsp;", " ");
		}
		getJspContext().getOut().print(outputText);
	}
 
 public String safeHtmlEscInput(String src) {

		if (src == null) {
			return "";
		}

		StringBuilder out = new StringBuilder();

		int len = src.length();

		for (int i = 0; i < len; i++) {
			switch (src.charAt(i)) {
			case '<':
				out.append("&#60;");
				break;
			case '>':
				out.append("&#62;");
				break;
			case '"':
				out.append("&#34;");
				break;
			case '\'':
				out.append("&#39;");
				break;
			case '|':
				out.append("&#124;");
				break;
			case '&':
				if (i + 1 < len && src.charAt(i + 1) == '#') {
					out.append(src.charAt(i));
				} else {
					out.append("&#38;");
				}
				break;
			case '*':
				out.append("&#42;");
				break;
			case '/':
				out.append("&#47;");
				break;
			case '\t':
				out.append("&#32;&#32;&#32;");
				break;
			case ' ':
				out.append("&#32;");
				break;
			default:
				out.append(src.charAt(i));
			}
		}

		return out.toString();
	}
 public String safeHtmlEscHidden(String src) {

		if (src == null) {
			return "";
		}

		StringBuilder out = new StringBuilder();

		int len = src.length();

		for (int i = 0; i < len; i++) {
			switch (src.charAt(i)) {
			case '<':
				out.append("&#60;");
				break;
			case '>':
				out.append("&#62;");
				break;
			case '"':
				out.append("&#34;");
				break;
			case '\'':
				out.append("&#39;");
				break;
			case '|':
				out.append("&#124;");
				break;
			case '\n':
				out.append("<BR>");
				break;
			case '&':
				if (i + 1 < len && src.charAt(i + 1) == '#') {
					out.append(src.charAt(i));
				} else {
					out.append("&#38;");
				}
				break;
			case '*':
				out.append("&#42;");
				break;
			case '/':
				out.append("&#47;");
				break;
			case '\t':
				out.append("&#32;&#32;&#32;");
				break;
			case ' ':

 				out.append("&#32;");
				break;
			default:
				out.append(src.charAt(i));
			}
		}

		return out.toString();
	}
 public String safeHtmlEscDisplay(String src) {

		if (src == null) {
			return "";
		}

		StringBuilder out = new StringBuilder();

		int len = src.length();

		for (int i = 0; i < len; i++) {
			switch (src.charAt(i)) {
			case '<':
				out.append("&lt;");
				break;
			case '>':
				out.append("&gt;");
				break;
			case '"':
				out.append("&quot;");
				break;
			case '\'':
				out.append("&#39;");
				break;
			case '|':
				out.append("&#124;");
				break;
			case '&':
				if (i + 1 < len && src.charAt(i + 1) == '#') {
					out.append(src.charAt(i));
				} else {
					out.append("&amp;");
				}
				break;
			case '*':
				out.append("&#42;");
				break;
			case '/':
				out.append("&#47;");
				break;
			case '\n':
				out.append("<BR>");
				break;
			case '\t':
				out.append("&nbsp;&nbsp;&nbsp;");
				break;
			case ' ':
				out.append("&nbsp;");
				break;
			default:
				out.append(src.charAt(i));
			}
		}

		return out.toString();
	}
}

